package com.example.coreshopapp.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.example.coreshopapp.R;
import com.example.coreshopapp.bean.OrderListBean;
import com.example.coreshopapp.utils.OrderingUtils;

import java.util.ArrayList;
import java.util.List;

public class OrderFormAdapter extends RecyclerView.Adapter<OrderFormAdapter.ViewHolder> {
    private List<OrderListBean> orderList = new ArrayList<>();

    public OrderFormAdapter(List<OrderListBean> orderList) {
        this.orderList = orderList;
    }
    public void updateOrderFormAdapter(List<OrderListBean> orderList) {
        this.orderList = orderList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public OrderFormAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_order_form, parent,false);
        return  new ViewHolder(inflate);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull OrderFormAdapter.ViewHolder holder, int position) {
        OrderListBean item = orderList.get(position);
        holder.codeTv.setText(item.getOrderId() +"");
        holder.classBillNoTv.setText(item.getClassBillNo());
        holder.placePersonTv.setText(item.getCreator() +"");
        holder.payMethodName.setText(item.getPaymentName());
        holder.priceTv.setText(item.getOrderAmount() +"");
        holder.actualTv.setText(item.getPayedAmount() +"");
        holder.quantityTv.setText(item.getItems().size() +"");
        holder.timeTv.setText(item.getPaymentTime() +"");
        holder.entrynoTv.setText(position + 1 +"");
        holder.itemView.setOnClickListener(view -> {
            if(onItemClickListener != null) {
                onItemClickListener.onItemClick(item.getOrderId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }
    public class ViewHolder extends  RecyclerView.ViewHolder {
        private TextView nameTv,codeTv,specTv,priceTv,quantityTv,payMethodName,actualTv,timeTv,placePersonTv,classBillNoTv,entrynoTv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
//            nameTv = itemView.findViewById(R.id.tv_name);
            codeTv = itemView.findViewById(R.id.tv_code);
//            specTv = itemView.findViewById(R.id.tv_unit);
            quantityTv = itemView.findViewById(R.id.tv_qty);
            payMethodName = itemView.findViewById(R.id.tv_pay_method_name); //计算后价格
            priceTv = itemView.findViewById(R.id.tv_price); // 原价
            actualTv = itemView.findViewById(R.id.tv_actualprice); // 售价
            timeTv = itemView.findViewById(R.id.tv_time); // 支付时间
            placePersonTv = itemView.findViewById(R.id.tv_place_person); // 支付时间
            classBillNoTv = itemView.findViewById(R.id.tv_classBillNo);
            entrynoTv = itemView.findViewById(R.id.tv_entryno);
        }
    }

    // 1.定义自定义点击事件
    public interface OnItemClickListener {
        void onItemClick(String orderId);
    }
    //    2.初始化点击时间
    public OrderFormAdapter.OnItemClickListener onItemClickListener;
    // 3. 定义构造函数方法，在主页面onCreate点击出发时传递回来，在再点击事件判断是否调用，有调用，把当前索引传递
    public void setOnItemClickListener(OrderFormAdapter.OnItemClickListener onMyItemClickListener) {
        onItemClickListener = onMyItemClickListener;
    }
}
